package com.shunda.ysdgj.manager.controller.goods;

import com.shunda.ysdgj.manager.service.goods.GoodsSpuService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsSpuFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsSpu;
import com.shunda.ysdgj.model.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "产品SPU接口")
@RestController
@RequestMapping("/api/v1/admin/goods/spu")
@RequiredArgsConstructor
public class GoodsSpuController {

    private final GoodsSpuService goodsSpuService;

    @Operation(summary = "根据ID获取简单产品SPU")
    @GetMapping("vo/{id}")
    public ResultVo vo(@PathVariable Long id) {
        return ResultVo.success(goodsSpuService.getSelectVoById(id));
    }

    @Operation(summary = "根据标题获取产品SPU列表")
    @GetMapping("getByTitle/{title}")
    public ResultVo getByTitle(@PathVariable String title) {
        return ResultVo.success(goodsSpuService.findByTitle(title));
    }

    @Operation(summary = "分页获取产品SPU列表")
    @PostMapping("list")
    public ResultVo list(@RequestBody GoodsSpuFindDto goodsSpuFindDto) {
        return ResultVo.success(goodsSpuService.findByPage(goodsSpuFindDto));
    }

    @Operation(summary = "添加产品SPU")
    @PostMapping("add")
    public ResultVo add(HttpServletRequest request, @RequestBody GoodsSpu goodsSpu) {
        String token = request.getHeader("token");
        if (!goodsSpuService.add(token, goodsSpu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改产品SPU")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody GoodsSpu goodsSpu) {
        if (!goodsSpuService.modify(goodsSpu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "产品SPU上架或下架")
    @PostMapping("modifySaleable")
    public ResultVo modifySaleable(@RequestBody GoodsSpu goodsSpu) {
        if (!goodsSpuService.modifySaleable(goodsSpu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "产品SPU有效或无效")
    @PostMapping("modifyValid")
    public ResultVo modifyValid(@RequestBody GoodsSpu goodsSpu) {
        if (!goodsSpuService.modifyValid(goodsSpu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除产品SPU")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!goodsSpuService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
